<script setup>
import { Delete, Edit, Lock, Plus } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { ref, reactive, onMounted } from 'vue';
import {get, post, put, del} from '../../api/http'

const roleList = reactive([])
const keyword = ref('')
const showDialog = ref(false)
const role = ref({})
const selectedList = reactive({arr:[]})
const isEdit = ref(false)
const loading = ref(false)

const getRoleList = () => {
    loading.value = true
    get('role/list',{keyword:keyword.value,pageNum:1,pageSize:10}).then(res=>{
        if(res.status){
            roleList.value = res.data.list
        }else{
            ElMessage({type:'error',message:res.msg})
        }
        loading.value = false
    })
}

const submitRole = () => {
    if(isEdit.value){
        put('role/set', role.value).then(res=>{
            ElMessage({type:res.status?'success':'error', message: res.msg})
            if(res.status){
                getRoleList()
                showDialog.value = false
            }
        })
    }else{
        post('role/add', role.value).then(res=>{
            ElMessage({type:res.status?'success':'error', message: res.msg})
            if(res.status){
                getRoleList()
                showDialog.value = false
            }
        })
    }
    
}

const editRole = (data) => {
    role.value = data
    showDialog.value = true
    isEdit.value = true
}

const setRole = () => {
    put('role/set', role.value).then(res=>{
        ElMessage({type:res.status?'success':'error', message: res.msg})
        if(res.status){
            getRoleList()
        }
    })
}

const removeRole = (data) => {
    selectedList.arr = [data.id]
    ElMessageBox.confirm('该操作将彻底删除角色信息，是否继续？','提示',{type:'warning'}).then(()=>{
        del('role/del', selectedList.arr).then(res=>{
            ElMessage({type:res.status?'success':'error', message: res.msg})
            if(res.status){
                getRoleList()
            }
        })
    })
}

const resetRoleForm = () => {
    role.value = {}
}

onMounted(()=>{
    getRoleList()
})

</script>
<template>
    <main v-blog-title data-title="角色管理">
        <div class="toolBar">
            <el-input v-model="keyword" @input="getRoleList" style="width: 300px; margin-right: 10px;" placeholder="关键字搜索"></el-input>
            <el-button @click="showDialog=true; isEdit=false;resetRoleForm();" type="primary" :icon="Plus">添加角色</el-button>
        </div>
        <div class="content" v-loading="loading">
            <el-table :data="roleList.value" border>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="英文标识" prop="roleCode"></el-table-column>
                <el-table-column label="权重" prop="roleWeight"></el-table-column>
                <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template #default="scope">
                        <el-button @click="editRole(scope.row)" type="primary" :icon="Edit" plain size="small">编辑</el-button>
                        <el-button @click="removeRole(scope.row)" type="danger" :icon="Delete" plain size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog width="30%" v-model="showDialog" title="角色信息">
            <el-form>
                <el-form-item label="角色名称">
                    <el-input v-model="role.roleName" placeholder="角色名称"></el-input>
                </el-form-item>
                <el-form-item label="角色标识">
                    <el-input v-model="role.roleCode" placeholder="角色标识"></el-input>
                </el-form-item>
                <el-form-item label="角色权重">
                    <el-input v-model="role.roleWeight" type="Number" placeholder="角色权重"></el-input>
                </el-form-item>
                <div style="width: 100%; text-align: right;">
                    <el-button @click="showDialog=false">取消</el-button>
                    <el-button @click="submitRole" type="primary">确定</el-button>
                </div>
            </el-form>
        </el-dialog>
    </main>
</template>
<style scoped>
.toolBar{
    padding: 10px;
}
.content{
    margin: 10px;
}
</style>